// 导入ex模块
const express = require('express')
const { salelist,addsale ,getSaleList} = require('./sale_data')

//2.创建路由对象
const router = express.Router()
router.get('/salelist', (request, response) => {
  // console.info('get方法获取到了', request.query)
  // console.info('get方法获取到了', request.params)
  let back = {
    "total": salelist.length,
    "rows": salelist,
    "code": 200,
    "msg": "查询成功"
  }
  response.send(back)
})
// 根据分页方式来获取
router.get('/getSaleList/:page/:size', (request, response) => {
  let page =  request.params.page
  let size =  request.params.size
  let salelist = getSaleList(page,size)
  let back = {
    "total": salelist.length,
    "rows": salelist,
    "code": 200,
    "msg": "查询成功"
  }
  response.send(back)
})

// 根据id 获取销售的数据
router.get('/getsale/:id', (request, response) => {
  let sale_id = request.params.id
  let sale = salelist.find((item) => {
    return item.id ==sale_id
  })
  let back = {
    "data": sale,
    "code": 200,
    "msg": "查询成功"
  }
  response.send(back)
})
// 添加房屋信息
router.get('/addsale', (request, response) => {
  let model = JSON.parse(request.query.model) 
  let sale = addsale(model)
  let back = {
    "data": sale,
    "code": 200,
    "msg": "查询成功"
  }
  response.send(back)
})
//向外导出路由对象
module.exports = router